﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using KCMS.BusinessLogic.Models;

namespace KCMS.Areas.Admin.Controllers
{
    public class OrderItemController : Controller
    {

        private IOrderItemService orderItemService = ObjectFactory.GetInstance<IOrderItemService>();
        private ICalculator calculator = ObjectFactory.GetInstance<ICalculator>();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult  GetByOrderId(Guid orderId){
            
            return PartialView("List",orderItemService.getByOrder(orderId));
        }
        public ContentResult GetTotalByOrder(Guid orderId)
        {
            var items=orderItemService.getByOrder(orderId);
            double total=calculator.getTotal(items);
            return Content(total.ToString());
        }
        public JsonResult Save(IEnumerable<OrderItem> items)
        {
            List<OrderItem> orderItems = items.ToList();
            orderItems.ForEach(delegate(OrderItem item)
            {
                int quantity = item.Quantity;
                item = orderItemService.getById(item.Id);
                item.Quantity = quantity;
                orderItemService.update(item);
            });
            return new JsonResult();
        }
        public JsonResult Delete(Guid id)
        {
            orderItemService.delete(id);
            return new JsonResult();
        }
    }
}
